index 50b8221..1aded29 100644
diff --git a/miniupnpc/miniwget.c b/miniupnpc/miniwget.c
index a420d57..a75f55b 100644
--- a/miniwget.c
+++ b/miniwget.c
@@ -162,7 +162,7 @@ getHTTPResponse(int s, int * size)
 							chunked = 1;
 						}
 					}
-					while(header_buf[i]=='\r' || header_buf[i] == '\n')
+					while((i < (int)header_buf_used) && (header_buf[i]=='\r' || header_buf[i] == '\n'))
 						i++;
 					linestart = i;
 					colon = linestart;
